﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace mKoenig.PictureSlide
{
  public class Resizer
  {
    private static mKoenig.PictureSlide.Properties.Settings setting = mKoenig.PictureSlide.Properties.Settings.Default;

    public static Image ResizeImage(Image imgToResize, Size size)
    {
      int sourceWidth = imgToResize.Width;
      int sourceHeight = imgToResize.Height;

      float nPercent = 0;
      float nPercentW = 0;
      float nPercentH = 0;

      nPercentW = ((float)size.Width / (float)sourceWidth);
      nPercentH = ((float)size.Height / (float)sourceHeight);

      if (nPercentH < nPercentW)
        nPercent = nPercentH;
      else
        nPercent = nPercentW;

      int destWidth = (int)(sourceWidth * nPercent);
      int destHeight = (int)(sourceHeight * nPercent);

      // holds the result
      Bitmap b = new Bitmap(size.Width, size.Height);
      Graphics g = Graphics.FromImage((Image)b);
      g.InterpolationMode = InterpolationMode.Bicubic;
      // fill with black
      g.FillRectangle(new SolidBrush(Color.Black), 0, 0, size.Width, size.Height);

      if (setting.BlurBackground)
      {
        // height bigger then width
        int destTmpWidth = size.Width;

        float factor = (float)size.Width / (float)imgToResize.Width;
        int destTmpHeight = (int)((float)imgToResize.Height * factor);

        if (destHeight < size.Height)
        {
          //factor = (float)size.Height / (float)destTmpHeight;
          //destTmpHeight = size.Height;
          //destTmpWidth = (int)((float)destTmpWidth * factor);
        }

        if (imgToResize.Height < imgToResize.Width)
        {
          factor = (float)size.Height / (float)destTmpHeight;
          destTmpHeight = size.Height;
          destTmpWidth = (int)((float)destTmpWidth * factor);
        }

        Rectangle destTmp = new Rectangle(new Point((size.Width - destTmpWidth) / 2, (size.Height - destTmpHeight) / 2), new Size(destTmpWidth, destTmpHeight));

        Bitmap b2 = new Bitmap(imgToResize.Width / 10, imgToResize.Height / 10);
        Graphics tmp2 = Graphics.FromImage((Image)b);

        Graphics g2 = Graphics.FromImage((Image)b2);
        g2.InterpolationMode = InterpolationMode.Low;
        g2.DrawImage(imgToResize, 0, 0, imgToResize.Width / 10, imgToResize.Height / 10);
        g2.Dispose();

        g.DrawImage(b2,
                    destTmp, 0, 0, b2.Width, b2.Height,
                    GraphicsUnit.Pixel);

        tmp2.Dispose();
        b2 = null;
      }

      // if pan zoom possible ?
      if ((destWidth > destHeight) && (((float)destWidth / size.Width * 100) > 70.0f) && setting.PanStretch)
      {
        destWidth = size.Width;
        float factor = (float)size.Width / (float)imgToResize.Width;
        destHeight = (int)((float)imgToResize.Height * factor);

        Rectangle dest = new Rectangle(new Point((size.Width - destWidth) / 2, (size.Height - destHeight) / 2), new Size(destWidth, destHeight));
        g.DrawImage(imgToResize,
                    dest, 0, 0, imgToResize.Width, imgToResize.Height,
                    GraphicsUnit.Pixel);
      }
      else
      {
        // draw result
        Rectangle dest = new Rectangle(new Point((size.Width - destWidth) / 2, (size.Height - destHeight) / 2), new Size(destWidth, destHeight));
        g.DrawImage(imgToResize,
                    dest, 0, 0, imgToResize.Width, imgToResize.Height,
                    GraphicsUnit.Pixel);

      }

      // return the result
      g.Dispose();
      return (Image)b;
    }
  }
}
